#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;
const ll inf = 2e18;

int n;
int ch[N][26], fail[N], ndn = 1;
ll val[N];
char s[N];
ll f[2][N], ans[N];

void insert(char *s, int tval) {
	int n = strlen(s + 1), p = 1;
	rep(i, 1, n) {
		int x = s[i] - 'a';
		if(!ch[p][x]) ch[p][x] = ++ndn;
		p = ch[p][x];
	}
	val[p] += tval;
}

const int LIM = 26 * 26;

void build() {
	queue < int > q; rep(i, 0, 25) ch[0][i] = 1; q.ep(1);
	while(q.size()) {
		int x = q.front(), F = fail[x]; val[x] += val[F]; q.pop();
		rep(i, 0, 25) {
			int &y = ch[x][i];
			if(!y) y = ch[F][i];
			else fail[y] = ch[F][i], q.ep(y);
		}
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in; rep(i, 1, n) scanf("%s", s + 1), insert(s, in);
	build();
	int o = 0; rep(i, 1, ndn) f[o][i] = -inf; f[o][1] = 0;
	ans[0] = -inf;
	rep(i, 1, LIM * 2) {
		ans[i] = -inf;
		rep(j, 1, ndn) f[o ^ 1][j] = -inf;
		rep(j, 1, ndn) rep(t, 0, 25) 
			chkmax(f[o ^ 1][ch[j][t]], f[o][j] + val[ch[j][t]]);
		rep(j, 1, ndn) chkmax(ans[i], f[o ^ 1][j]);
		o ^= 1; chkmax(ans[i], ans[i - 1]);
	}
	if(ans[LIM] == inf || ans[LIM * 2] > ans[LIM]) puts("Infinity");
	else printf("%lld\n", ans[LIM]);
	return 0;
}
